package cn.cutie.clotconfig.demo;

import cn.cutie.clotconfig.client.annotation.EnableClotConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@SpringBootApplication
@EnableConfigurationProperties(value = {ClotDemoConfig.class})
@EnableClotConfig
@RestController
public class ClotConfigDemoApplication {

    @Value("${clot.a}")
    private String a;

    @Value("${clot.b}")
    private String b;

    @Autowired
    private ClotDemoConfig clotDemoConfig;

    @Autowired
    Environment environment;

    public static void main(String[] args) {
        SpringApplication.run(ClotConfigDemoApplication.class, args);
    }

    @GetMapping("/demo")
    public String demo() {
        return "clot.a=" + a + "\n" +
                "clot.b=" + b + " \n" +
                "demo.a=" + clotDemoConfig.getA() + " \n" +
                "demo.b=" + clotDemoConfig.getB() + " \n";
    }

    @Bean
    ApplicationRunner runner() {
        // 需要塞属性到environment中
        System.out.println(Arrays.toString(environment.getActiveProfiles()));
        return args -> {
            System.out.println(a);
            System.out.println(clotDemoConfig.getA());
        };
    }

}
